home *** CD-ROM | disk | FTP | other *** search
Wrap
#include <clib/exec_protos.h> #include <clib/dos_protos.h> #include <clib/intuition_protos.h> #include <exec/memory.h> #include <string.h> #include <stdlib.h> #include "Tools.h" extern void Quit(void); struct MemNode { struct MemNode *child; /* Next block in list */ char *file; /* File that allocated us */ ULONG line; /* Line we were allocated on */ ULONG size; /* Size of memory block */ }; static struct MemNode *head = NULL; /****************************************************************************************************************/ /**** ****/ /** DisplayMsg **/ /**** ****/ /****************************************************************************************************************/ void DisplayMsg(char *Msg) { struct EasyStruct RequestMsg = { sizeof(struct EasyStruct), 0, "GenCode C Error", "%s", "OK" }; EasyRequest(NULL,&RequestMsg,NULL,Msg); } /****************************************************************************************************************/ /**** ****/ /** safe_AllocMemory **/ /**** ****/ /****************************************************************************************************************/ void *safe_AllocMemory(ULONG byteSize,char *File,ULONG Line) { static char msg[80]; struct MemNode *t; if (head) t = head; else t = NULL; if (!(head = AllocVec(byteSize + sizeof(struct MemNode),MEMF_PUBLIC|MEMF_CLEAR))) { sprintf(msg,"ERROR !!!!! NOT ENOUGH MEMORY !!!\nCan't allocate %ld bytes\n",byteSize); DisplayMsg(msg); return NULL; } head->child = t; head->file = File; head->line = Line; head->size = byteSize; return ((void *)((ULONG)head + sizeof(struct MemNode))); } /****************************************************************************************************************/ /**** ****/ /** safe_FreeMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_FreeMemory(void *ptr,char* File,ULONG Line) { static char msg[81]; struct MemNode *t1,*t2; if (ptr == NULL) return; t1 = head; t2 = NULL; while(t1) { if((ULONG)ptr == (ULONG)t1 + sizeof(struct MemNode)) { if(t2) t2->child = t1->child; /* Remove block from the list */ else head = t1->child; FreeVec(t1); return; } t2 = t1; t1 = t1->child; } sprintf(msg,"%s %ld: Free twice (?) FreeVec($%lx)",File,Line,(ULONG)ptr - sizeof(struct MemNode)); DisplayMsg(msg); safe_ShowMemory(File,Line); } /****************************************************************************************************************/ /**** ****/ /** safe_ShowMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_ShowMemory(char* File,ULONG Line) { struct MemNode *t; BPTR con; if(head) { if (!(con = Open("CON:0/10/400/100/ShowMemory .../AUTO/CLOSE/WAIT/INACTIVE",MODE_NEWFILE))) return; t = head; FPrintf(con, "%s %ld: Memory List\n", File, Line); FPrintf(con," %-12s %-12s %-16s %-4s\n","Address", "Size", "File", "Line"); FPrintf(con," %-12s %-12s %-16s %-4s\n","-------", "----", "----", "----"); while(t) { FPrintf(con," $%-11lx %-12ld %-16s %-4ld\n",t,t->size,t->file,t->line); t = t->child; } } } /****************************************************************************************************************/ /**** ****/ /** safe_ClearMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_ClearMemory(char* File,ULONG Line) { static char msg[210]; struct MemNode *t1, *t2; if(head) { t1 = head; sprintf(msg, "Oups !! there is a BUG (a memory allocation problem)\nPlease report this bug to GenCode'sAuthors\nwith the next parameters (in ShowMemory Window)\n%s %ld: Freeing Memory List", File, Line); DisplayMsg(msg); safe_ShowMemory(File,Line); while(t1) { t2 = t1->child; FreeVec(t1); t1 = t2; } } head = NULL; } /****************************************************************************************************************/ /**** ****/ /** CopyBlock **/ /**** ****/ /****************************************************************************************************************/ char *CopyBlock(FILE *file,char *Filechar,char *String, char *begin,char *end, char *MsgErrorBegin,char *MsgErrorEnd, char *MainFile) { char ctmp; char *str,*str1; if (!(str = strstr(String,begin))) { char *msg; if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorBegin)+strlen(MainFile)))) { fclose(file); FreeMemory(Filechar); Quit(); } sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorBegin); DisplayMsg(msg); FreeMemory(msg); fclose(file); FreeMemory(Filechar); Quit(); } if (end) { if (!(str1 = strstr(str,end))) { char *msg; if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorEnd)+strlen(MainFile)))) { fclose(file); FreeMemory(Filechar); Quit(); } sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorEnd); DisplayMsg(msg); FreeMemory(msg); fclose(file); FreeMemory(Filechar); Quit(); } ctmp=*str1; *str1='\0'; fprintf(file,"%s",str+strlen(begin)); *str1=ctmp; return str1; } else { fprintf(file,"%s",str+strlen(begin)); return NULL; } } /****************************************************************************************************************/ /**** ****/ /** Indent **/ /**** ****/ /****************************************************************************************************************/ void Indent(FILE *file,int nb) { int i; for(i=0;i<nb;i++) fprintf(file, "\t"); } /****************************************************************************************************************/ /**** ****/ /** extract_dir **/ /**** ****/ /****************************************************************************************************************/ void extract_dir( char *filename ) { if (*filename) { filename=PathPart(filename); *filename='\0'; } } /****************************************************************************************************************/ /**** ****/ /** extract_file **/ /**** ****/ /****************************************************************************************************************/ void extract_file( char * path, char * filename ) { strcpy(filename,FilePart(path)); } /****************************************************************************************************************/ /**** ****/ /** add_extend **/ /**** ****/ /****************************************************************************************************************/ void add_extend( char *filename, char * extend ) { strcat(filename,extend); } /****************************************************************************************************************/ /**** ****/ /** remove_extend **/ /**** ****/ /****************************************************************************************************************/ void remove_extend( char *filename ) { char *aux; aux = strrchr(FilePart(filename),'.'); if (aux) *aux='\0'; } /****************************************************************************************************************/ /**** ****/ /** change_extend **/ /**** ****/ /****************************************************************************************************************/ void change_extend( char *filename, char * extend ) { remove_extend(filename); add_extend(filename,extend); } /****************************************************************************************************************/ /**** ****/ /** LoadFileInRam **/ /**** ****/ /****************************************************************************************************************/ /* Read a file and load it in memory */ char * LoadFileInRAM(char *file,BOOL Quiet) { BPTR TMPfile; __aligned struct FileInfoBlock Info; char *adr_file = NULL; int size; if (TMPfile = Open(file, MODE_OLDFILE)) { ExamineFH(TMPfile, &Info); size = Info.fib_Size; if (!(adr_file = AllocMemory(size+1))) { Close(TMPfile); return NULL; } Read( TMPfile, adr_file, size); adr_file[size] = '\0'; Close(TMPfile); return adr_file; } else { if (!Quiet) { char *msg; if (!(msg=AllocMemory(12+strlen(file)+1))) return NULL; sprintf(msg,"Can't open %s\n",file); DisplayMsg(msg); FreeMemory(msg); } return NULL; } } /****************************************************************************************************************/ /**** ****/ /** GetCurrentDirectory **/ /**** ****/ /****************************************************************************************************************/ char * GetCurrentDirectory(void) { BPTR lock; UWORD len = 512; char *CurrentDirectory = NULL; if (!(lock = Lock("PROGDIR:",ACCESS_READ))) { DisplayMsg("ERROR !!!!! CAN'T OBTAIN A LOCK TO THE CURRENT DIRECTORY !!!\n"); return NULL; } do { if (CurrentDirectory) FreeMemory(CurrentDirectory); if (!(CurrentDirectory = AllocMemory(len+1))) { UnLock(lock); return NULL; } NameFromLock(lock,CurrentDirectory,len); len*=2; }while(IoErr()==ERROR_LINE_TOO_LONG); UnLock(lock); return CurrentDirectory; } /****************************************************************************************************************/ /**** ****/ /** CopyFile **/ /**** ****/ /****************************************************************************************************************/ BOOL CopyFile(char *FromFile,char *ToFile) { char *buff; char *msg; BPTR File; __aligned struct FileInfoBlock Info; if (!(File = Open(FromFile,MODE_OLDFILE))) { if (!(msg = AllocMemory(strlen(FromFile)+1+13))) { return FALSE; } sprintf(msg,"Can't find \"%s\"",FromFile); DisplayMsg(msg); FreeMemory(msg); return FALSE; } ExamineFH(File, &Info); if (!(buff = AllocMemory(Info.fib_Size))) { Close(File); return FALSE; } Read(File,buff,Info.fib_Size); Close(File); if (!(File = Open(ToFile,MODE_NEWFILE))) { if (!(msg = AllocMemory(strlen(ToFile)+1+13))) { return FALSE; } sprintf(msg,"Can't write \"%s\"",ToFile); DisplayMsg(msg); FreeMemory(msg); return FALSE; } Write(File,buff,Info.fib_Size); FreeMemory(buff); Close(File); return TRUE; }